package com.xmwzw.catpu;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;

import com.mikepenz.fastadapter.FastAdapter;
import com.mikepenz.fastadapter.IAdapter;
import com.mikepenz.fastadapter.IItem;
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter;
import com.xmwzw.catpu.bean.SoftBean;
import com.xmwzw.catpu.databinding.ActivityResultBinding;
import com.xmwzw.catpu.http.BaseRequest;
import com.xmwzw.catpu.http.listener.SearchListener;
import com.xmwzw.catpu.item.SoftItem;
import com.xmwzw.catpu.util.Constant;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : wangzw
 * @date : 20-4-17上午11:15
 * @desc :
 */
public class SearchResultActivity extends BaseActivity {
    private ActivityResultBinding resultBinding;
    private FastItemAdapter adapter = new FastItemAdapter();
    private List<SoftItem> items = new ArrayList<>();
    private SearchHandler handler;


    @Override
    public int getLayoutId() {
        return R.layout.activity_result;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new SearchHandler(this);
        resultBinding = getBaseViewBinding();

        resultBinding.rvResult.setLayoutManager(new LinearLayoutManager(this));
        resultBinding.etSearch.addTextChangedListener(new MyTextWatcher());
        resultBinding.rvResult.setAnimation(null);
        resultBinding.rvResult.setAdapter(adapter);
        adapter.set(items);

        resultBinding.setShowLoading(false);

        adapter.withOnClickListener(new FastAdapter.OnClickListener() {
            @Override
            public boolean onClick(View v, IAdapter adapter, IItem item, int position) {
                SoftItem item1 = (SoftItem) item;

                Intent intent = new Intent(SearchResultActivity.this, SoftDetailActivity.class);
                intent.putExtra(Constant.KEY_SOFT_BEAN, item1.getSoftBean());
                startActivity(intent);
                return false;
            }
        });


    }


    private class MyTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            items.clear();
            String content = editable.toString();
            if (TextUtils.isEmpty(content)) {
                adapter.set(items);
                adapter.notifyAdapterDataSetChanged();
                return;
            }
            resultBinding.setShowLoading(true);
            BaseRequest.getInstance().search(content, new SearchListener() {
                @Override
                public void getSearch(List<SoftBean> softBeans) {
                    if (softBeans == null) {
                        items.clear();
                        Message message = handler.obtainMessage();
                        message.obj = items;
                        handler.sendMessage(message);
                        return;
                    }
                    for (SoftBean softBean : softBeans) {
                        items.add(new SoftItem().fillData(true, softBean));
                    }

                    Message message = handler.obtainMessage();
                    message.obj = items;
                    handler.sendMessage(message);

                }
            });

        }
    }

    private static class SearchHandler extends Handler {
        private WeakReference<SearchResultActivity> weakReference;

        public SearchHandler(SearchResultActivity activity) {
            weakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (weakReference.get() == null) {
                return;
            }

            Log.d("tag", "handleMessage: ");
            weakReference.get().adapter.notifyDataSetChanged();
            weakReference.get().adapter.notifyAdapterDataSetChanged();

            List<SoftItem> softItems = (List<SoftItem>) msg.obj;
            weakReference.get().adapter.set(softItems);
            weakReference.get().adapter.notifyAdapterDataSetChanged();
            weakReference.get().resultBinding.setShowLoading(false);
        }
    }
}
